#include		<stdio.h>
#include		<stdlib.h>
#include		<sys/types.h>
#include		<sys/stat.h>
#include		<fcntl.h>
#include		<string.h>
#include		<unistd.h>

#include		"list.h"
#include		"heap.h"
#include		"ftrace.h"

typedef			struct s_lol
{
  int			fd_file;
  s_list		*list_node;
  s_list		*list_link;
}			t_lol;

t_lol			g_lol;

void			prepare_graph()
{
  g_lol.list_node = NULL;
  g_lol.list_link = NULL;
  g_lol.fd_file = open("graph.dot", O_CREAT | O_WRONLY | O_TRUNC, 0666);
}

void			generate_dot_file()
{
  s_list		*tmp;

  write(g_lol.fd_file, "graph G {\n", 10);
  tmp = g_lol.list_node;
  while (tmp)
    {
      write(g_lol.fd_file, tmp->value, strlen(tmp->value));
      tmp = tmp->next;
    }
  tmp = g_lol.list_link;
  while (tmp)
    {
      write(g_lol.fd_file, tmp->value, strlen(tmp->value));
      tmp = tmp->next;
    }
  write(g_lol.fd_file, "}\n", 2);
}

void			add_node_graph(s_func *func)
{
  char			buff[1024];

  if (!func->is_syscall)
    sprintf(buff, "%lu [label=\"%s  [call : %lx sym : %lx]\", shape=box, color=green];\n",
	    func->id, func->name, func->call_addr, func->sym_addr);
  else
    sprintf(buff, "%lu [label=\"%s  [call : %lx]\", shape=invtriangle, color=red];\n",
	    func->id, func->name, func->call_addr);
  g_lol.list_node = list_add(&(g_lol.list_node), strdup(buff));
}

void			add_node_link(unsigned int idf, unsigned int ids)
{
  char			buff[1024];

  sprintf(buff, "%u -- %u\n", idf, ids);
  g_lol.list_link = list_add(&(g_lol.list_link), strdup(buff));
}
